﻿Public Class frmType

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New frmTypeeditor
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            loaddata()
        End If
    End Sub
    Public Sub loaddata()
        Cmd.Parameters("@S") = "%" & TextBox1.Text & "%"
        Me.DataGridView1.DataSource = Cmd.ExecuteDataTable("SELECT * FROM Type WHERE TypeId LIKE @S AND Active=1;")
    End Sub

    Private Sub frmType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        loaddata()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = DataGridView1.SelectedRows(0)
        Dim frm As New frmTypeeditor
        frm.txttypeid.Text = row.Cells("TypeId").Value
        frm.txttype.Text = row.Cells("TypeName").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            loaddata()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row = DataGridView1.SelectedRows(0)
        Dim id = row.Cells(0).Value

        Cmd.Parameters("@Id") = id
        Cmd.ExecuteNonQuery("UPDATE Type SET Active=0 WHERE TypeId=@Id")

        loaddata()
    End Sub
End Class